package com.pengshun.modules.jwt;

import com.pengshun.modules.domain.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  2019/1/14.
 */
@Component
public class JwtTokenUtil {

    private String secret="pc-api";

    private int expiration=864000000;

    public String generateToken(User info) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("uiId", info.getId());
        claims.put("name", info.getName());
        claims.put("phone", info.getPhone());
        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
                .signWith(SignatureAlgorithm.HS512, secret) // 不使用公钥私钥
//                .signWith(SignatureAlgorithm.RS256, secret)
                .compact();
    }

    public Claims parseToken(String token) {
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey(secret) // 不使用公钥私钥
//                    .setSigningKey(publicKey)
                    .parseClaimsJws(token).getBody();
            return claims;
        } catch (Exception e) {
        }
        return null;
    }


}
